package com.adobe.apprise.commands
{
	import com.adobe.apprise.database.*;
	import com.adobe.apprise.events.RefreshCountsEvent;
	import com.adobe.apprise.model.ModelLocator;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.collections.ArrayCollection;
	
	public class PopulateViewControlCommand implements ICommand
	{
		public function execute(ce:CairngormEvent):void
		{
			this.getFeeds();
		}

		private function getFeeds():void
		{
			var ml:ModelLocator = ModelLocator.getInstance();
			ml.feeds = new ArrayCollection();
			var feedsResponder:DatabaseResponder = new DatabaseResponder();
			feedsResponder.addEventListener(DatabaseEvent.RESULT_EVENT,
				function(feedsEvent:DatabaseEvent):void
				{
					ml.feeds.source = feedsEvent.data;
					getAuthors();
				});
			ml.db.getFeeds(feedsResponder);

		}

		private function getAuthors():void
		{
			var ml:ModelLocator = ModelLocator.getInstance();
			ml.authors = new ArrayCollection();
			var authorsResponder:DatabaseResponder = new DatabaseResponder();
			authorsResponder.addEventListener(DatabaseEvent.RESULT_EVENT,
				function(authorEvent:DatabaseEvent):void
				{
					ml.authors.source = authorEvent.data;
					getTopics();
				});
			ml.db.getAuthors(authorsResponder);

		}

		private function getTopics():void
		{
			var ml:ModelLocator = ModelLocator.getInstance();
			ml.topics = new ArrayCollection();
			var topicsResponder:DatabaseResponder = new DatabaseResponder();
			topicsResponder.addEventListener(DatabaseEvent.RESULT_EVENT,
				function(topicsEvent:DatabaseEvent):void
				{
					ml.topics.source = topicsEvent.data;
					new RefreshCountsEvent().dispatch();
				});
			ml.db.getTopics(topicsResponder);
		}
	}
}
